package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolRedPack;
import com.sc.nft.entity.dto.WebEcologyPoolRedPackPageDO;
import com.sc.nft.enums.ecologypool.EcologyPoolRedPackAuditStatusEnum;
import com.sc.nft.mapper.EcologyPoolRedPackMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 品牌红包(sc_dao_redpack)数据DAO
 *
 * @author jinpinhai
 * @since 2024-08-10 11:40:15
 */
@Slf4j
@Repository
public class EcologyPoolRedPackDao extends ServiceImpl<EcologyPoolRedPackMapper, EcologyPoolRedPack> {

    public Boolean updateRedPackById(Long id, EcologyPoolRedPack ecologyPoolRedPack) {
        LambdaUpdateWrapper<EcologyPoolRedPack> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyPoolRedPack::getId, id);
        return update(ecologyPoolRedPack, wrapper);
    }

    public List<EcologyPoolRedPack> listByEndTimeAfter() {
        LambdaQueryWrapper<EcologyPoolRedPack> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(EcologyPoolRedPack::getEndTime, new Date());
        wrapper.eq(EcologyPoolRedPack::getAuditStatus, EcologyPoolRedPackAuditStatusEnum.APPROVED);
        wrapper.orderByDesc(EcologyPoolRedPack::getId);
        return list(wrapper);
    }

    public List<EcologyPoolRedPack> listByRefundPoint() {
        LambdaQueryWrapper<EcologyPoolRedPack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolRedPack::getRefundStatus, Boolean.FALSE);
        wrapper.lt(EcologyPoolRedPack::getEndTime, new Date());
        wrapper.eq(EcologyPoolRedPack::getAuditStatus, EcologyPoolRedPackAuditStatusEnum.APPROVED);
        wrapper.orderByDesc(EcologyPoolRedPack::getId);
        return list(wrapper);
    }

    public Page<WebEcologyPoolRedPackPageDO> redPackPage(Long daoId, Page<EcologyPoolRedPack> page) {
        List<WebEcologyPoolRedPackPageDO> webEcologyPoolRedPackPageDOS = baseMapper.redPackPage(page, daoId);
        Page<WebEcologyPoolRedPackPageDO> webEcologyPoolRedPackPageDOPage = new Page<>();
        webEcologyPoolRedPackPageDOPage.setTotal(page.getTotal());
        webEcologyPoolRedPackPageDOPage.setCurrent(page.getCurrent());
        webEcologyPoolRedPackPageDOPage.setSize(page.getSize());
        webEcologyPoolRedPackPageDOPage.setRecords(webEcologyPoolRedPackPageDOS);
        return webEcologyPoolRedPackPageDOPage;
    }

    public Page<EcologyPoolRedPack> page(Page<EcologyPoolRedPack> page, Long daoId, String name, EcologyPoolRedPackAuditStatusEnum ecologyPoolRedPackAuditStatusEnum) {
        LambdaQueryWrapper<EcologyPoolRedPack> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(daoId), EcologyPoolRedPack::getDaoId, daoId);
        queryWrapper.like(StringUtils.isNotBlank(name), EcologyPoolRedPack::getName, name);
        queryWrapper.eq(Objects.nonNull(ecologyPoolRedPackAuditStatusEnum), EcologyPoolRedPack::getAuditStatus, ecologyPoolRedPackAuditStatusEnum);
        queryWrapper.orderByDesc(EcologyPoolRedPack::getId);
        return page(page, queryWrapper);
    }

    public int redPackStatus(Long daoId) {
        return baseMapper.redPackStatus(daoId);
    }
}